﻿using System;
using System.IO;
using System.Xml.Linq;

namespace YArchitech.MEP
{
	public class HYDuctSystemOption : HYSystemOption
	{
		public double DesignVelocity_Main { get; set; }

		public double DesignVelocity_Side { get; set; }

		public double DesignVelocity_SideSdie { get; set; }

		public double DesignVelocity_Terminal { get; set; }

		public bool MainRangeEnable { get; set; }

		public HYRange MainRange
		{
			get
			{
				return this.m_rangeMain;
			}
			set
			{
				this.m_rangeMain = value;
			}
		}

		public bool SideRangeEnable { get; set; }

		public HYRange SideRange
		{
			get
			{
				return this.m_rangeSide;
			}
			set
			{
				this.m_rangeSide = value;
			}
		}

		public bool RectRactorEnable { get; set; }

		public double RectFactor { get; set; }

		public double RectFactorDeviation { get; set; }

		public HYDuctSystemOption()
		{
			this.DesignVelocity_Main = 6.0;
			this.DesignVelocity_Side = 4.0;
			this.DesignVelocity_SideSdie = 3.5;
			this.DesignVelocity_Terminal = 2.0;
			this.MainRangeEnable = false;
			this.m_rangeMain = default(HYRange);
			this.SideRangeEnable = false;
			this.m_rangeSide = default(HYRange);
			this.RectRactorEnable = false;
			this.RectFactor = 4.0;
			this.RectFactorDeviation = 0.5;
			base.TerminalLossEnable = false;
			base.TerminalLoss = 50.0;
			base.ShockFix = 1.0;
		}

		public double GetVelocity(HYBranchPlace place)
		{
			double result = this.DesignVelocity_Main;
			switch (place)
			{
			case HYBranchPlace.Main:
				result = this.DesignVelocity_Main;
				break;
			case HYBranchPlace.Side:
				result = this.DesignVelocity_Side;
				break;
			case HYBranchPlace.SideSide:
				result = this.DesignVelocity_SideSdie;
				break;
			}
			return result;
		}

		public override void WriteXml(XElement xe)
		{
			base.WriteXml(xe);
			xe.SetAttributeValue("DesignVelocity_Main", this.DesignVelocity_Main);
			xe.SetAttributeValue("DesignVelocity_Side", this.DesignVelocity_Side);
			xe.SetAttributeValue("DesignVelocity_SideSdie", this.DesignVelocity_SideSdie);
			xe.SetAttributeValue("DesignVelocity_Terminal", this.DesignVelocity_Terminal);
			xe.SetAttributeValue("MainRangeEnable", this.MainRangeEnable);
			xe.SetAttributeValue("SideRangeEnable", this.SideRangeEnable);
			xe.SetAttributeValue("RectRactorEnable", this.RectRactorEnable);
			xe.SetAttributeValue("RectFactor", this.RectFactor);
			xe.SetAttributeValue("RectFactorDeviation", this.RectFactorDeviation);
			XElement xelement = xe.Element("MainRange");
			if (xelement == null)
			{
				xelement = new XElement("MainRange");
				xe.Add(xelement);
			}
			this.m_rangeMain.WriteXml(xelement);
			XElement xelement2 = xe.Element("SideRange");
			if (xelement2 == null)
			{
				xelement2 = new XElement("SideRange");
				xe.Add(xelement2);
			}
			this.m_rangeSide.WriteXml(xelement2);
		}

		public override void ReadXml(XElement xe)
		{
			base.ReadXml(xe);
			this.DesignVelocity_Main = xe.ToDouble("DesignVelocity_Main", 5.0);
			this.DesignVelocity_Side = xe.ToDouble("DesignVelocity_Side", 4.0);
			this.DesignVelocity_SideSdie = xe.ToDouble("DesignVelocity_SideSdie", 3.5);
			this.DesignVelocity_Terminal = xe.ToDouble("DesignVelocity_Terminal", 2.0);
			this.MainRangeEnable = xe.ToBool("MainRangeEnable", false);
			this.SideRangeEnable = xe.ToBool("SideRangeEnable", false);
			this.RectRactorEnable = xe.ToBool("RectRactorEnable", false);
			this.RectFactor = xe.ToDouble("RectFactor", 4.0);
			this.RectFactorDeviation = xe.ToDouble("RectFactorDeviation", 0.5);
			XElement xe2 = xe.Element("MainRange");
			this.m_rangeMain.ReadXml(xe2);
			XElement xe3 = xe.Element("SideRange");
			this.m_rangeSide.ReadXml(xe3);
		}

		public virtual void Save(string filename)
		{
			XElement xelement;
			if (File.Exists(filename))
			{
				xelement = XElement.Load(filename);
			}
			else
			{
				Directory.CreateDirectory(Path.GetDirectoryName(filename));
				xelement = new XElement("HYUserConfig");
			}
			if (xelement == null)
			{
				return;
			}
			XElement xe = xelement.ChildElement("DuctSystemOption");
			this.WriteXml(xe);
			xelement.Save(filename);
		}

		public virtual void Load(string filename)
		{
			XElement xelement = XElement.Load(filename);
			if (xelement == null)
			{
				return;
			}
			XElement xelement2 = xelement.Element("DuctSystemOption");
			if (xelement2 == null)
			{
				return;
			}
			this.ReadXml(xelement2);
		}

		private HYRange m_rangeMain;

		private HYRange m_rangeSide;
	}
}
